package com.louis.iread.ui;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.louis.iread.R;
import com.louis.iread.adapter.ArticleListAdapter;
import com.louis.iread.bean.Article;
import com.louis.iread.bean.Book;
import com.louis.iread.bean.BookMgr;
import com.louis.iread.skin.SkinMgr;
import com.louis.iread.utils.ConstValue;

import org.xutils.ex.DbException;

import java.util.List;

import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class ArticleListActivity extends SwipeBackActivity {
    private SwipeBackLayout mSwipeBackLayout;
    private Book mBook;
    private List<Article> mArticleList;
    private ArticleListAdapter mAdapter;
    private ListView mListView;
    private int mArticleIndex = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SkinMgr.getInstance().register(this);
        setContentView(R.layout.content_article_list);
        //实现沉浸式状态栏
        if (Build.VERSION.SDK_INT >= 19) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        //设置右划关闭页面
        mSwipeBackLayout = getSwipeBackLayout();
        mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_ALL);
        ImageView backView= (ImageView) findViewById(R.id.back);
        backView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initBook();
        ImageButton top = (ImageButton) findViewById(R.id.to_top);
        top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListView.setSelection(0);
            }
        });
        ImageButton bottom = (ImageButton) findViewById(R.id.to_bottom);
        bottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListView.setSelection(mArticleList.size() - 1);
            }
        });
    }

    protected void initBook() {
        Intent intent = getIntent();

        mArticleIndex = intent.getIntExtra(ConstValue.ARTICLE_INDEX, 0);

        long bookIndex = intent.getLongExtra(ConstValue.BOOK_INDEX, -1);
        if (bookIndex == -1) {
            finish();
        }
        mBook = BookMgr.getMgr().getBook(bookIndex);
        if (mBook == null) {
            finish();
        }
        if (!mBook.isRead()) {
            mBook.setRead(true);
            BookMgr.getMgr().updateBook(mBook);
        }
        TextView textView = (TextView) findViewById(R.id.novel_name);
        textView.setText(mBook.getNovelName());

        //如果获取章节目录失败,则退出
        try {
            mArticleList = mBook.getArticles();
        } catch (DbException e) {
            e.printStackTrace();
            Toast.makeText(this, R.string.get_contents_failed, Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        initArticleList();
    }

    protected void initArticleList() {
        mAdapter = new ArticleListAdapter(this, R.layout.article_list_item, mArticleList, com.louis.iread.BR.article, mArticleIndex);
        mListView = (ListView) findViewById(R.id.article_list);
        mListView.setAdapter(mAdapter);
        mListView.setSelection(mArticleIndex);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.putExtra("article_index", position);
                ArticleListActivity.this.setResult(ConstValue.OPEN_ARTICLE_LIST_ACK, intent);
                ArticleListActivity.this.finish();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SkinMgr.getInstance().unregister(this);
    }
}
